import {createRouter, createWebHistory} from "vue-router";

// @ts-ignore
import loginIndex from './view/Home/Index.vue';
// @ts-ignore
import resgister from './components/register.vue';
// @ts-ignore
import login from './components/loginacc.vue';
// @ts-ignore
import homeindex from './view/Home/Index.vue';
// @ts-ignore
import userEdit from './components/userEdit.vue';
// @ts-ignore
import animalEdit from "./components/animalEdit.vue";

// @ts-ignore
import animalRecord from "./components/animalRecord.vue";
// @ts-ignore
import feedRecord from "./components/feedRecord.vue";
// @ts-ignore
import medicalRecord from "./components/medicalRecord.vue";
// @ts-ignore
import Home from "./components/homeIndex.vue";
// @ts-ignore
import doctorWorkRecord from "./components/doctorWorklRecord.vue";
// @ts-ignore
import animalWatch from "./components/animalWatch.vue";





const router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:'',
            component:login,

        },
        {
            path:'/index',
            component:homeindex,
            children:[
                {
                    path:'',
                    component:Home
                },
                {
                    path:'useredit',
                    component:userEdit
                },
                {
                    path:'animaledit',
                    component:animalEdit
                },
                {
                    path:'animalrecord',
                    component:animalRecord
                },
                {
                    path:'feedrecord',
                    component:feedRecord
                },
                {
                    path:'medicalrecord',
                    component:medicalRecord
                },
                {
                    path:'doctorWorkRecord',
                    component:doctorWorkRecord
                },
                {
                    path:'animalWatch',
                    component:animalWatch
                },

            ],
        },

    ]


});
// @ts-ignore
//实现路由的再定向 当token失效时 页面会一直返回login
router.beforeEach((to, from, next) => {
    // @ts-ignore
    const isAuthenticated = JSON.parse(localStorage.getItem('user'));
    if (to.path !== '/' && !isAuthenticated) {
        // 如果用户未经认证且尝试访问受保护的路由，
        // 将其重定向到登录页面
        next('/');
    } else {
        next();
    }
});

export default router;
